package com.omenkk.juc.synchronizedPlus;

import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class demo1 {
    private static  boolean haveSmoke=false;
    private static  boolean haveWater=false;
private static final Object lock=new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            log.debug("有烟没,{}",haveSmoke);
            synchronized(lock){
                while(!haveSmoke){
log.debug("没烟，先歇会");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if(haveSmoke)  log.debug("有烟了，开始干活");
                else log.debug("没干成活");
            }

        },"小名");

        Thread t2=new Thread(()->{
            log.debug("有水没,{}",haveSmoke);
            synchronized(lock){
                while(!haveWater){
                    log.debug("没水，先歇会");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                if(haveWater)log.debug("有水了，开始干活");
                else log.debug("没水，不干活了");
            }

        },"小红");
t1.start();
t2.start();
        Thread.sleep(2000);
        Thread t3=new Thread(()->{
            synchronized(lock) {
                log.debug("送烟了");
                haveSmoke=true;
                lock.notifyAll();
            }
        },"送烟的");
        t3.start();


     Thread.sleep(2000);
    Thread t4=new Thread(()->{
        synchronized(lock) {
            log.debug("送水了");
            haveWater=true;
            lock.notifyAll();
        }
    },"送水的");
        t4.start();

}
}
